import * as areaApi from '@/api/area.js';

export default {
  namespaced: true,
  state() {
    return {
      areaList: [],
      hasNext: false,
    };
  },
  mutations: {
    SET_AREA_LIST(state, areaList) {
      state.areaList = areaList;
    },
    SET_HAS_NEXT(state, newValue) {
      state.hasNext = newValue;
    },
  },
  actions: {
    async updateAreaList({ commit }) {
      const res = await areaApi.getAreaList();
      commit('SET_AREA_LIST', res.data);
      commit('SET_HAS_NEXT', true);
    },
    async getAreaChildren(ctx, code) {
      const current = ctx.state.areaList.find((a) => a.code === code);
      ctx.commit('SET_AREA_LIST', current.areaList);
      ctx.commit('SET_HAS_NEXT', !!current.areaList);
    },
  },
};
